<?php


namespace app\api\controller;


use app\api\validate\UserValidate;
use app\api\model\UserModel;
use app\BaseController;
use think\Request;

class User extends BaseController
{
    public function register(Request $request)
    {
        $data = $request->param();
        //验证器
        $validate = new UserValidate();
        $val = $validate->scene('validate')->check($data);
        if(!$val)
        {
            return error($validate->getError());
        }
        //密码加密
        $str = CreateStr();
        $data['salt'] = $str;
        $data['password'] = md5($data['password'].$str);
        //入库
        $res = UserModel::create($data);
        if(UserModel::create($data))
        {
            return success('注册成功');
        }else{
            return error('注册失败');
        }
    }
    public function login(Request $request)
    {
        $mobile = $request->param('mobile');
        $password = $request->param('password');
        if(empty($mobile))
        {
            return error('手机号码不能为空');
        }
        if(empty($password))
        {
            return error('密码不能为空');
        }
        $res = UserModel::where('mobile','=',$mobile)
            ->find();
        if(empty($res))
        {
            return error('手机号错误');
        }
        if($res['password'] == md5($password.$res['salt']))
        {
            return success('登录成功');
        }else{
            return error('密码错误');
        }
    }
}